package com.yowasa.xiuxian.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.entity.bo.user.UserAttr;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bo.user.UserBattle;
import com.yowasa.xiuxian.entity.bo.user.UserConfig;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.service.RedisService;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import com.yowasa.xiuxian.util.PagescreenshotUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutionException;



@RequestMapping("/test")
@RestController
public class TestController {
    public static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Autowired
    private RedisService redisService;

    @RequestMapping("/screen")
    public String screen(Long id,String villaId,Integer roomId) throws IOException, TemplateException, ExecutionException, InterruptedException {
        UserBO byId = userHelper.getById(id);
        XiuXianExtend byAccountId = extendHelper.getByAccountId(id);
        // 创建一个 Configuration 对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        // 设置模板文件所在的路径

        configuration.setDirectoryForTemplateLoading( ResourceUtils.getFile("classpath:image/html"));
        // 加载模板文件
        Template template = configuration.getTemplate("template.html","UTF-8");

        // 创建一个数据模型
        Map<String, Object> dataModel = new HashMap<>();
        String mental = StringUtils.isBlank(byId.getUserConfig().getMental()) ? "无" : byId.getUserConfig().getMental();
        String lingGen = ElementEnum.getLingGenDesc(byId.getElement(), XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY);
        String levelName = StageEnum.getById(byId.getStage()).getName();
        dataModel.put("user", byId);
        dataModel.put("mental", mental);
        dataModel.put("lingGen", lingGen);
        dataModel.put("levelName", levelName);

        dataModel.put("userExtend", byAccountId);
        String imgaeUrl = "https://img-static.mihoyo.com/communityweb/avatar/avatar30024.png";
        dataModel.put("imageUrl", imgaeUrl);
        String join = "赞无";
        dataModel.put("specialEffect", join);
        dataModel.put("lockEndTime", "当前处于空闲状态");
        // 渲染模板并将结果输出到字符串中
        StringWriter writer = new StringWriter();
        template.process(dataModel, writer);
        long time = new Date().getTime();
        File file = ResourceUtils.getFile("classpath:image/gen/");
        String absolutePath = file.getAbsolutePath()+"/";
        FileOutputStream fileOutputStream = new FileOutputStream(absolutePath+time+".html");
        fileOutputStream.write(writer.toString().getBytes());
        PagescreenshotUtil.screenShotWx(absolutePath+time+".html", "C:\\opt\\pic\\mysPic\\"+time+".jpg");

        SendMessage sendMessage = new SendMessage();
        sendMessage.setVilla_id(villaId);
        sendMessage.setRoom_id(roomId);
        String url = "http://121.4.252.124:8080/images/pic/mysPic/"+time+".jpg";
        try {
            messageHelper.imageSendMys(sendMessage,url);
            return "success";
        }catch (Exception e){
            return "fail";
        }
    }

    @RequestMapping("getMemberRoles")
    public String getMemberRoles(String villaId,Integer roomId){
        SendMessage sendMessage = new SendMessage();
        sendMessage.setVilla_id(villaId);
        sendMessage.setRoom_id(roomId);
        return messageHelper.getMemberRoles(sendMessage);
    }

    @RequestMapping("tranferImage")
    public String tranferImage(String url,String villaId,Integer roomId){
        SendMessage sendMessage = new SendMessage();
        sendMessage.setVilla_id(villaId);
        sendMessage.setRoom_id(roomId);
        return messageHelper.tranferImage(sendMessage,url);
    }

//    public static void main(String[] args) throws IOException, TemplateException {
//        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
//
//
//        // 设置模板文件所在的路径
//        configuration.setDirectoryForTemplateLoading( ResourceUtils.getFile("classpath:image/html"));
//
//        // 加载模板文件
//        Template template = configuration.getTemplate("template.html");
//
//        // 创建一个数据模型
//        Map<String, Object> dataModel = new HashMap<>();
////        dataModel.put("username", "Hello, World!");
////        dataModel.put("mylist", Lists.newArrayList("aaa","bbb","ccc"));
//        UserBO bo = new UserBO();
//        bo.setName("牛逼");
//        bo.setStage(1);
//        bo.setLevel(0);
//        bo.setUserAttr(UserAttr.genAttr());
//        UserBattle userBattle = UserBattle.genBattleAttr(bo.getUserAttr());
//        bo.setUserBattle(userBattle);
//        bo.setUserOriginBattle(userBattle);
//        bo.setElement(ElementEnum.genLingGen());
//        bo.setUserConfig(new UserConfig());
//        bo.setStartAt(new Date());
//        bo.setAge(DateXUtil.START_AGE);
//        bo.buildBaseInfo();
//        String mental = "我操";
//        String lingGen = "傻呗";
//        String levelName = "那没事了";
//        dataModel.put("user", bo);
//        dataModel.put("mental", mental);
//        dataModel.put("lingGen", lingGen);
//        dataModel.put("levelName", levelName);
//        // 渲染模板并将结果输出到字符串中
//        StringWriter writer = new StringWriter();
//        template.process(dataModel, writer);
//        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\yoto\\Desktop\\fujian\\test.html");
//        fileOutputStream.write(writer.toString().getBytes());
//    }

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("100", "200", "300", "400");
        int i = strings.indexOf(200+"");
        System.out.println(i);
    }
}
